import 'package:enjoy_plus/pages/home/components/home_list.dart';
import 'package:enjoy_plus/pages/home/components/home_nav.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:flutter/material.dart';


import '../../utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 重写，要触发父类的方法
  @override
  void initState() {
    super.initState();
    getData();
  }

List notifyList = [];

// 发送请求获取数据函数
  getData() async {
    // 发送请求获取数据
    try {
      final res =   await http.get('/announcement');
      setState(() {
        notifyList = res['data']; //没有数据类型，使用[]得到数据,
      });
      ToastUtil.showSuccess( '请求成功');
    }catch(e) {
      print('请求失败 :$e');
      ToastUtil.showError('请求失败');
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        children:  [
          // 导航条
          HomeNav(),
          // 中间广告图
          ClipRRect(
            borderRadius: BorderRadius.circular(10.0),
            child: Image.asset('images/banner@2x.jpg'),
          ),
          // 社区公告
           HomeList(notifyList: notifyList,)
        ],
      )
    );
  }
}